package com.example.dingding.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import com.example.dingding.config.HttpHelper;
import com.example.dingding.entity.UseCar;
import com.example.dingding.mapper.UseCarMapper;
import com.example.dingding.service.UseCarService;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import  com.aliyun.dingtalkworkflow_1_0.models.*;
import java.util.*;


import com.aliyun.dingtalkworkflow_1_0.*;

/**
 * @author 2979163039
 * @description 针对表【t_use_car】的数据库操作Service实现
 * @createDate 2024-02-26 11:46:43
 */
@Service
public class UseCarServiceImpl extends ServiceImpl<UseCarMapper, UseCar> implements UseCarService {
    //审批实例id
    private String instanceId;

    @Autowired
    HttpHelper httpHelper;

    //发起审批,详见dingdingOA审批
    @Override
    public String saveApproval(UseCar useCar,String requestAuthCode,List<String> divisionLeader,List<String> logistiServiceLeadership,List<String> logisticslineManager,List<String> logisticsFleet) {
//        String requestAuthCode
        String access_token = httpHelper.getAccess_Token();
//        // 获取用户信息
        // 获取用户信息
        DingTalkClient clients = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getuserinfo");
        OapiUserGetuserinfoRequest request = new OapiUserGetuserinfoRequest();
        request.setCode(requestAuthCode);
        request.setHttpMethod("GET");
        OapiUserGetuserinfoResponse response = new OapiUserGetuserinfoResponse();
        try {
            response = clients.execute(request, access_token);

        } catch (ApiException e) {
            e.printStackTrace();

        }
        // 查询得到当前用户的userId
        // 获得到userId之后应用应该处理应用自身的登录会话管理（session）,避免后续的业务交互（前端到应用服务端）每次都要重新获取用户身份，提升用户体验
        String user = response.getUserid();



        // 发起审批
        com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues
                formComponentValues1
                =
                new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues()
                        .setName("TextField-title")
                        .setValue(useCar.getDegreeUrgency())
                        .setComponentType("DDMultiSelectField");

        com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues
                formComponentValues2
                =
                new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues()
                        .setName("TextField-title2")
                        .setValue(useCar.getRider());


        com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues
                formComponentValues3
                =
                new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues()
                        .setName("TextField-title3")
                        .setValue(useCar.getInitiatorPhone());

        com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues
                formComponentValues4
                =
                new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues()
                        .setName("TextField-title4")
                        .setValue(useCar.getVehicleCategory())
                        .setComponentType("DDMultiSelectField");



        com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues
                formComponentValues6
                =
                new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues()
                        .setName("TextareaField-6")
                        .setValue(useCar.getCarDriven());
//                        .setComponentType("TextareaField");

        com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues
                formComponentValues7
                =
                new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues()
                        .setName("TextField-title7")
                        .setValue(useCar.getSpecificDestination());

        //日期类型（审批判断条件）
      //1
      com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValuesDetailsDetails r1 = new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValuesDetailsDetails()
        .setId("TextField-title8")
        .setBizAlias("日期类型")
        .setName("日期类型")
        .setValue("工作日")
        .setComponentType("DDMultiSelectField");
      com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValuesDetailsDetails r2 = new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValuesDetailsDetails()
        .setId("TextField-title8")
        .setBizAlias("日期类型")
        .setName("日期类型")
        .setValue("周末")
        .setComponentType("DDMultiSelectField");
      com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValuesDetailsDetails r3 = new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValuesDetailsDetails()
        .setId("TextField-title8")
        .setBizAlias("日期类型")
        .setName("日期类型")
        .setValue("法定节假日")
        .setComponentType("DDMultiSelectField");
//2
      com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValuesDetails r11 = new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValuesDetails()
        .setId("TextField-title8")
        .setBizAlias("日期类型")
        .setName("日期类型")
        .setValue("工作日")
        .setDetails(java.util.Arrays.asList(
          r1
        ));

      com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValuesDetails r22 = new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValuesDetails()
        .setId("TextField-title8")
        .setBizAlias("日期类型")
        .setName("日期类型")
        .setValue("工作日")
        .setDetails(java.util.Arrays.asList(
          r2
        ));

      com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValuesDetails r33 = new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValuesDetails()
        .setId("TextField-title8")
        .setBizAlias("日期类型")
        .setName("日期类型")
        .setValue("工作日")
        .setDetails(java.util.Arrays.asList(
          r3
        ));

        com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues
                formComponentValues8
                =
                new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues()
                        .setName("TextField-title8")
                        .setComponentType("DDMultiSelectField")
                        .setValue(useCar.getWorkingDay())
                  .setDetails(java.util.Arrays.asList(
                    r11,r22,r33
                  ));

//目的地区域（审批判断条件）
      //1
      com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValuesDetailsDetails q1 = new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValuesDetailsDetails()
        .setId("TextField-title5")
        .setBizAlias("点击选择目的地区域")
        .setName("目的地区域")
        .setValue("长沙区域")
        .setComponentType("DDMultiSelectField");
      com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValuesDetailsDetails q2 = new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValuesDetailsDetails()
        .setId("TextField-title5")
        .setBizAlias("点击选择目的地区域")
        .setName("目的地区域")
        .setValue("省内区域")
        .setExtValue("省外区域")
        .setComponentType("DDMultiSelectField");
      //2
      com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValuesDetails q11 = new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValuesDetails()
        .setId("TextField-title5")
        .setBizAlias("点击选择目的地区域")
        .setName("目的地区域")
        .setValue("长沙区域")
        .setDetails(java.util.Arrays.asList(
         q1
        ));
      com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValuesDetails  q22 = new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValuesDetails()
        .setId("TextField-title5")
        .setBizAlias("点击选择目的地区域")
        .setName("目的地区域")
        .setValue("省内区域")
        .setExtValue("省内区域")
        .setDetails(java.util.Arrays.asList(
          q2
        ));
      //3
      com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues
        formComponentValues5
        =
        new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues()
          .setName("TextField-title5")
          .setComponentType("DDMultiSelectField")
          .setValue(useCar.getDestinationArea())
          .setDetails(java.util.Arrays.asList(
            q11,q22
          ));



        com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues
                formComponentValues9
                =
                new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues()
                        .setName("TextField-title9")
                        .setComponentType("DDMultiSelectField")
                        .setValue(useCar.getVehicleType());

        com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues
                formComponentValues10
                =
                new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues()
                        .setName("DDDateRangeField-10")
                        .setValue(useCar.getUseTime());

        com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues
                formComponentValues11
                =
                new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues()
                        .setName("DDDateRangeField-11")
                        .setValue(useCar.getReturnTime());

        com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues
                formComponentValues12
                =
                new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues()
                        .setName("TextField-title12")
                        .setValue(useCar.getDepartment());


        com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues
                formComponentValues13
                =
                new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues()
                        .setName("TextField-title13")
                        .setComponentType("DDMultiSelectField")
                        .setValue(useCar.getStartingPoint());


        com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues
                formComponentValues14
                =
                new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues()
                        .setName("TextField-title14")
                        .setComponentType("DDMultiSelectField")
                        .setValue(useCar.getExpenses());
//缴费金额
        com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues
                formComponentValues15
                =
                new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues()
                        .setName("TextField-title15")
                        .setValue("由车管填写");
//司机电话
        com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues
                formComponentValues16
                =
                new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues()
                        .setName("TextField-title16")
                        .setValue("由车管填写");

//车牌号
        com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues
                formComponentValues17
                =
                new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues()
                        .setName("TextField-title17")
                        .setValue("由车管填写");

        com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues
                formComponentValues18
                =
                new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues()
                        .setName("TextField-title18")
                        .setValue("由车管填写");

        com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues
                formComponentValues19
                =
                new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues()
                        .setName("TextField-title19")
                        .setValue(useCar.getRiderNumber());


        com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues
                formComponentValues20
                =
                new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues()
                        .setName("TextField-title20")
                        .setValue("由车管填写");


        com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues
                formComponentValues21
                =
                new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues()
                        .setName("TextField-title21")
                        .setValue("由车管填写");

        com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues
                formComponentValues22
                =
                new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues()
                        .setName("TextField-title22")
                        .setValue("由车管填写");
//获取选择的单个审批人员
//        List<StartProcessInstanceRequest.StartProcessInstanceRequestApprovers> approvers = new ArrayList<>();
//        approvers.add(
//                new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestApprovers()
//                        .setActionType("OR")
//                        .setUserIds(
//                                //发送审批人员id给钉钉
//                             userids
//                        ));

        //处领导
        List<String> divisionHead=new ArrayList<>();
        //后勤处领导
        List<String> logisticsService=new ArrayList<>();
        //后勤分管行领导
        List<String> logisticsManager=new ArrayList<>();
        //后勤车队
        List<String> logisticsVechicleFleet=new ArrayList<>();

//        根据需求判断审批角色(未方便日后修改增加处领导和后勤车队的替换)
      if(useCar.getWorkingDay().equals("工作日")&&useCar.getDestinationArea().equals("长沙区域")){
          divisionHead.addAll(divisionLeader);
          logisticsVechicleFleet.addAll(logisticsFleet);
      }else if(useCar.getWorkingDay().equals("工作日")&&useCar.getDestinationArea().equals("省内区域")||useCar.getDestinationArea().equals("省外区域")){
          divisionHead.addAll(divisionLeader);
          logisticsService.addAll(logistiServiceLeadership);
         logisticsVechicleFleet.addAll(logisticsFleet);
      }else if(useCar.getWorkingDay().equals("周末")&&useCar.getWorkingDay().equals("法定节假日")||useCar.getDestinationArea().equals("长沙区域")){
          divisionHead.addAll(divisionLeader);
          logisticsService.addAll(logistiServiceLeadership);
          logisticsVechicleFleet.addAll(logisticsFleet);
      }else if(useCar.getWorkingDay().equals("周末")&&useCar.getWorkingDay().equals("法定节假日")||useCar.getDestinationArea().equals("省外区域")){
          divisionHead.addAll(divisionLeader);
          logisticsService.addAll(logistiServiceLeadership);
          logisticsManager.addAll(logisticslineManager);
          logisticsVechicleFleet.addAll(logisticsFleet);
      }else if (useCar.getWorkingDay().equals("周末")&&useCar.getWorkingDay().equals("法定节假日")||useCar.getDestinationArea().equals("省内区域")){
          divisionHead.addAll(divisionLeader);
          logisticsVechicleFleet.addAll(logisticsFleet);
      }




////处领导
      com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestApprovers approvers0 = new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestApprovers()
        .setActionType("OR")
        .setUserIds(
//                java.util.Arrays.asList(
//              "101461495621589373","manager5803","293607254434496260","2936076807694171"
                divisionHead
//
//                       "101461495621589373"
//                )
        );


////后勤处领导
      com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestApprovers approvers1 = new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestApprovers()
        .setActionType("OR")
         .setUserIds(
//         java.util.Arrays.asList(
//          "101461495621589373","manager5803","2936055737797847","293312695221669372"
                 logisticsService
//                 )
        );
////后勤分管行领导
      com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestApprovers approvers2 = new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestApprovers()
        .setActionType("OR")
        .setUserIds(
//                java.util.Arrays.asList(
//          "101461495621589373","manager5803","2936055737797847","293312695221669372","2936076807694171"
                logisticsManager
//        )
        );

////后勤车队
      com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestApprovers approvers3 = new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest.StartProcessInstanceRequestApprovers()
        .setActionType("OR")
        .setUserIds(
//         java.util.Arrays.asList(
//            "2936055737797847","manager5803"
                logisticsVechicleFleet
//                 "101461495621589373"
//       )
        );




    com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest startProcessInstanceRequest
            = new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest()

            .setApprovers(java.util.Arrays.asList(approvers0,approvers1,approvers2,approvers3))
            .setDeptId(1L)
            .setMicroappAgentId(2964367113L)
            .setOriginatorUserId(user)
            //获取抄送人id给钉钉
            //PROC-FF9276C4-9AAA-447C-AD10-0FEA43CBA601
//            .setProcessCode("PROC-8C7EA2C8-E6B9-4389-A406-DE513011CCC8")
            .setProcessCode("PROC-9A17758C-56E4-40F6-9F93-5235FCDB5B8D")
            .setFormComponentValues(java.util.Arrays.asList(
                    formComponentValues1,
                    formComponentValues2,
                    formComponentValues3,
                    formComponentValues4,
                    formComponentValues5,
                    formComponentValues6,
                    formComponentValues7,
                    formComponentValues8,
                    formComponentValues9,
                    formComponentValues10,
                    formComponentValues11,
                    formComponentValues12,
                    formComponentValues13,
                    formComponentValues14,
                    formComponentValues15,
                    formComponentValues16,
                    formComponentValues17,
                    formComponentValues18,
                    formComponentValues19,
                    formComponentValues20,
                    formComponentValues21,
                    formComponentValues22


            ));

        try {
            Client client = createClient();
            //审批流程设置部分
            com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceHeaders startProcessInstanceHeadert = new com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceHeaders();
            startProcessInstanceHeadert.xAcsDingtalkAccessToken = getAccessToken();
            StartProcessInstanceHeaders startProcessInstanceHeaders
                    = new StartProcessInstanceHeaders();
            startProcessInstanceHeaders.xAcsDingtalkAccessToken = getAccessToken();
            JSONObject.toJSONString(startProcessInstanceRequest);
            StartProcessInstanceResponse startProcessInstanceResponse = client.startProcessInstanceWithOptions(
                    startProcessInstanceRequest, startProcessInstanceHeaders,
                    new RuntimeOptions());
            //获取审批实例id
             instanceId = startProcessInstanceResponse.getBody().getInstanceId();

        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
                System.out.println(err.code);
                System.out.println(err.message);
            }
        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
                System.out.println(err.code);
                System.out.println(err.message);
            }
        }

        System.out.println("审批实例id：" + instanceId);
        return instanceId;
    }

    @Override
    public Client createClient() throws Exception{
        Config config = new Config();
        config.protocol = "https";
        config.regionId = "central";
        return new com.aliyun.dingtalkworkflow_1_0.Client(config);
    }

    @Override
    public String getAccessToken() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
        config.protocol = "https";
        config.regionId = "central";
        com.aliyun.dingtalkoauth2_1_0.Client client = new com.aliyun.dingtalkoauth2_1_0.Client(config);
        com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest getAccessTokenRequest
                = new com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest()
                .setAppKey("dingzvtl6gimsru7ymlx")
                .setAppSecret("Fj4xtc4l5cScmZhfNCkAcCMPFRA5XKq26BxDYOWCMQrra6iCSbmGuKIYkPrx9t9o");
        try {
            return client.getAccessToken(getAccessTokenRequest).getBody().getAccessToken();
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }

        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }

        }
        return null;
    }

    @Override
    public List<String> getPhone(List<String> userid) {
        String access_token = httpHelper.getAccess_Token();
        List<String> userResponses = new ArrayList<>();
        //获取用户详情
        for (String user: userid) {
            try {
                DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
                OapiV2UserGetRequest req = new OapiV2UserGetRequest();
                req.setUserid(user);
                req.setLanguage("zh_CN");
                OapiV2UserGetResponse rsp = client.execute(req, access_token);
                log.error("获取用户详情：" + rsp.getBody());

                JSONObject responseBody = JSON.parseObject(rsp.getBody());
                if (responseBody != null && responseBody.containsKey("result")) {
                    JSONObject result = responseBody.getJSONObject("result");
                    String phoneNumber = result.getString("mobile");
                    userResponses.add(phoneNumber);
                }

            } catch (ApiException e) {
                e.printStackTrace();
            }
        }


        return userResponses;
    }

    //根据角色的id获取用户的id
    @Override
    public List<String> getRole() {
        //获取token
        String access_token = httpHelper.getAccess_Token();
        //获取角色下的用户id
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/role/simplelist");
        //角色id,该值可以通过钉钉api接口获取角色详情
        List<String> role=new ArrayList<>();
        //处领导id
        role.add("4284757970");
        //后勤处领导
        role.add("4284920806");
        //后勤分管行领导
        role.add("4285031407");
        //后勤车队
        role.add("4285104159");

        //处领导
       List<String> divisionLeader=new ArrayList<>();

       //后勤处领导
       List<String> logistiServiceLeadership=new ArrayList<>();

       //后勤分管行领导
        List<String> logisticslineManager=new ArrayList<>();

        //后勤车队
        List<String> logisticsFleet=new ArrayList<>();

        List<String> leaderRole=new ArrayList<>();

        for(String r : role){
            OapiRoleSimplelistRequest req = new OapiRoleSimplelistRequest();
            req.setRoleId(Long.valueOf(r));
            req.setSize(20L);
            req.setOffset(0L);


            try {

                OapiRoleSimplelistResponse rsp = client.execute(req, access_token);
                JSONObject responseBody = JSON.parseObject(rsp.getBody());

                System.out.println(rsp.getBody());
                if (responseBody != null && responseBody.containsKey("result")) {

                    JSONObject result = responseBody.getJSONObject("result");
                    JSONArray list = result.getJSONArray("list");
                   for(int i=0;i<list.size();i++){
                       JSONObject item = (JSONObject) list.get(i);
//                       item.get("userid");
                       if (r=="4284757970"){
                           divisionLeader.add((String) item.get("userid"));

                       }else if(r=="4285031407"){
                           logistiServiceLeadership.add((String) item.get("userid"));

                       }else if (r=="4285104159"){
                           logisticslineManager.add((String) item.get("userid"));

                       }else{
                           logisticsFleet.add((String) item.get("userid"));

                       }
                   }
//                   leaderRole.add(String.valueOf(divisionLeader));
//                   leaderRole.add(String.valueOf(logistiServiceLeadership));
//                   leaderRole.add(String.valueOf(logisticslineManager));
//                   leaderRole.add(String.valueOf(logisticsFleet));
//                    System.out.println(leaderRole);
                }
            } catch (ApiException e) {
                throw new RuntimeException(e);
            }
        }
        leaderRole.add(String.valueOf(divisionLeader));
        leaderRole.add(String.valueOf(logistiServiceLeadership));
        leaderRole.add(String.valueOf(logisticslineManager));
        leaderRole.add(String.valueOf(logisticsFleet));
        System.out.println(leaderRole);

        return leaderRole;
    }

    @Override
    public String getUserPhone(String requestAuthCode) {
        String access_token = httpHelper.getAccess_Token();
//        // 获取用户信息
        // 获取用户信息
        DingTalkClient clients = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getuserinfo");
        OapiUserGetuserinfoRequest request = new OapiUserGetuserinfoRequest();
        request.setCode(requestAuthCode);
        request.setHttpMethod("GET");
        OapiUserGetuserinfoResponse response = new OapiUserGetuserinfoResponse();
        try {
            response = clients.execute(request, access_token);

        } catch (ApiException e) {
            e.printStackTrace();

        }
        // 查询得到当前用户的userId
        // 获得到userId之后应用应该处理应用自身的登录会话管理（session）,避免后续的业务交互（前端到应用服务端）每次都要重新获取用户身份，提升用户体验
        String user = response.getUserid();
        String phoneNumber = null;
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
            OapiV2UserGetRequest req = new OapiV2UserGetRequest();
            req.setUserid(user);
            req.setLanguage("zh_CN");
            OapiV2UserGetResponse rsp = client.execute(req, access_token);

            JSONObject responseBody = JSON.parseObject(rsp.getBody());
            if (responseBody != null && responseBody.containsKey("result")) {
                JSONObject result = responseBody.getJSONObject("result");
                String.valueOf(phoneNumber = result.getString("mobile"));
            }

        } catch (ApiException e) {
            e.printStackTrace();
        }
        
        return phoneNumber;
    }


}